<?php
include 'admin_passport_permission_lib.php';

if ($s_Action == 'update-permission') {

    // <editor-fold defaultstate="collapsed" desc="Update">
    get_passport_module_action($db_conn, $s_PPO_ModuleID, $s_PPO_ActionID, $a_filterings);
    $v_response_json       = new stdClass();
    $v_response_json->code = 0;
    $v_response_json->data = array ( );
    
    if($f_moduleactionid == ''){ echo json_encode($v_response_json, true); }
  
    $s_Where = "moduleactionid = $f_moduleactionid";
    
    db_delete_row($db_conn, 'passport_permission', "$s_Where and permissionfortype = '$f_permissionfortype'");
    
    if (!is_array($f_permissionfor)) { $f_permissionfor = array(); }

    $a_rows = $a_respone = array();
    
    foreach ($f_permissionfor as $v_item) {

        $a_rows[] = array (
            'moduleactionid'    => $f_moduleactionid,
            'permissionfor'     => $v_item,
            'permissionfortype' => $f_permissionfortype,
            'permissiontype'    => 'ALLOW',
            'updaterid'         => $s_UserID,
            'updatedate'        => $f_updatedate,
        );
    }

    if(!empty($a_rows)) { db_insert_row($db_conn, $a_rows, 'passport_permission'); }    
    
    $db_query  = "update passport_module_action set updaterid = '$s_UserID', updatedate = '$f_updatedate' where $s_Where";
    $db_result = db_query($db_conn, $db_query);
    
    // <editor-fold defaultstate="collapsed" desc="Response Data">
    if ($f_permissionfortype == 'GROUP') {
        foreach ($f_permissionfor as $v_item) {
            $a_respone[] = isset($a_PhanBan[$v_item]) ? $a_PhanBan[$v_item] : $v_item;
        }
    } else if ($f_permissionfortype == 'USER') {
        
        $db_query = "
        select userid, saint, username
        from user
        where $s_get_HUYNH_TRUONG_condition and userid in ('".implode("', '", $f_permissionfor)."') ";

        $db_result = db_query($db_conn, $db_query);

        while ($o_user = db_fetch($db_result)) {
            $a_respone[] = $o_user->saint . ' ' . $o_user->username;
        }
    }
    // </editor-fold>

    $v_response_json->code = 200;
    $v_response_json->data = show_data_list($a_respone);
    echo json_encode($v_response_json, true);
    exit;
    // </editor-fold>
}
else if ($s_Action == 'get_group_permission') {

    // <editor-fold defaultstate="collapsed" desc="Get Data">
    $v_response_json       = new stdClass();
    $v_response_json->code = 0;
    $v_response_json->data = array ( );
    
    if($f_moduleactionid == ''){ echo json_encode($v_response_json, true); }
    
    $a_permissions = $a_groupteams = array( );
    
    $db_query  = db_get_query($a_tablecolumns_namhoa_passport_permission) . " where moduleactionid = $f_moduleactionid and permissionfortype = 'GROUP' ";
    $db_result = db_query($db_conn, $db_query);

    while ($d_passport_permission = db_fetch($db_result)) {
        $a_permissions[$d_passport_permission->permissionfor] = $d_passport_permission->permissionfor;
    }
    
    foreach ($a_PhanBan as $key => $value) {

        $o_tmp           = new stdClass();
        $o_tmp->itemid   = $key;
        $o_tmp->itemname = $value;
        
        if (in_array($key, $a_permissions)) {
            $o_tmp->checked = true;
        }
        else {
            $o_tmp->checked = false;
        }

        $v_response_json->data[] = $o_tmp;
    }
    
    $v_response_json->moduleactionid    = $f_moduleactionid;
    $v_response_json->permissionfortype = 'GROUP';
    $v_response_json->code              = 200;
    echo json_encode($v_response_json, true);
    exit;
    // </editor-fold>
}
else if ($s_Action == 'get_user_permission') {

    // <editor-fold defaultstate="collapsed" desc="Get Data">
    $v_response_json       = new stdClass();
    $v_response_json->code = 0;
    $v_response_json->data = array ( );
    
    if($f_moduleactionid == ''){ echo json_encode($v_response_json, true); }
    
    $a_permissions = $a_groupteams = array( );
    
    $db_query  = db_get_query($a_tablecolumns_namhoa_passport_permission) . " where moduleactionid = $f_moduleactionid and permissionfortype = 'USER' ";
    $db_result = db_query($db_conn, $db_query);

    while ($d_passport_permission = db_fetch($db_result)) {
        $a_permissions[$d_passport_permission->permissionfor] = $d_passport_permission->permissionfor;
    }
    
    $db_query  = "
        select userid, saint, username
        from user
        where $s_get_HUYNH_TRUONG_condition ";
    
    $db_result = db_query($db_conn, $db_query);

    while ($o_user = db_fetch($db_result)) {

        $o_tmp           = new stdClass();
        $o_tmp->itemid   = $o_user->userid;
        $o_tmp->itemname = $o_user->saint . ' ' . $o_user->username;

        if (in_array($o_user->userid, $a_permissions)) {
            $o_tmp->checked = true;
        }
        else {
            $o_tmp->checked = false;
        }

        $v_response_json->data[] = $o_tmp;
    }
    
    $v_response_json->moduleactionid    = $f_moduleactionid;
    $v_response_json->permissionfortype = 'USER';
    $v_response_json->code              = 200;
    echo json_encode($v_response_json, true);
    exit;
    // </editor-fold>
}
else {
    raise_error('No Action.');
}

local_redirect('admin_passport_permission_list.php?' . $s_QueryString_Page);
?>